package com.example.bishe.web.c.vo;

import com.example.bishe.entity.RecordChildren;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 功能描述
 *
 * @author: LDH
 * @date: 2023年05月03日 17:44
 */
@Data
public class BarVO {

    int[] value;

    String time;

    public List<BarVO> setBar(List<RecordChildren> list){
        List<BarVO> barVOList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            int keepTime, intervalTime = 0;
            BarVO barVO = new BarVO();
            int[] value = new int[2];
            keepTime = getKeepTime(list.get(i).getCreateTime().toString(), list.get(i).getUpdateTime().toString());
            if (i != list.size()-1){
                intervalTime = getKeepTime(list.get(i).getCreateTime().toString(), list.get(i+1).getCreateTime().toString());
            }
            value[0] = keepTime+1;
            value[1] = intervalTime;
            barVO.setValue(value);
            barVO.setTime(StringUtils.substring(list.get(i).getCreateTime().toString(), 0, 10).replaceAll("-","\\/"));
            barVOList.add(barVO);
        }
        return barVOList;
    }

    private int getKeepTime(String start, String end){
        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date star = null;//开始时间
        Date endDay= null;//结束时间
        try {
            star = dft.parse(start);
            endDay = dft.parse(end);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Long starTime=star.getTime();
        Long endTime=endDay.getTime();
        Long num=endTime-starTime;//时间戳相差的毫秒数
//        System.out.println("相差天数为："+num/24/60/60/1000);//除以一天的毫秒数
        num = num/24/60/60/1000;
        return num.intValue();
    }

}